//
//  OrderManagerController.m
//  LazyCat
//
//  Created by jike on 16/6/12.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "OrderManagerController.h"
#import "WSsearchController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
@interface OrderManagerController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    UIWebView *_webView;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
}
@end

@implementation OrderManagerController
- (void)backAction:(UIButton *)btn {//判断是否可以HTML返回
    if (![_webView canGoBack]) {
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        [_webView goBack];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpMainView];
    [self loadMessPart];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navBar addSubview:_progressView];
}
- (void)setUpMainView {
    [self initNav:@"订单管理" doBack:YES];
    
    //搜索按钮
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth - 52, 30, 42, 30);
    
    [searchBtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setImage:[UIImage imageNamed:@"searchLog"] forState:UIControlStateNormal];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(4, 10, 4, 10)];
    [self.navBar addSubview:searchBtn];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
    NSURL *url = [NSURL URLWithString:@"http://210.14.79.98/UserCenter/index?username=pf_buy01"];
    _webView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
}
- (void)loadMessPart {
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
        //        if (_finishLoad) {
        //            _progressView.hidden = YES;
        //        }
    }];
}
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
        [_progressView removeFromSuperview];
    }];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _progressView.hidden = YES;
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"网络不给力"];
        _webView.hidden = YES;
        [_progressView setProgress:1 animated:YES];
    }
}

- (void)searchBtn {
    WSsearchController *searchVC  = [[WSsearchController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

@end
